package com.hnkywl.jip.action;

import com.google.gson.Gson;
import com.hnkywl.jip.service.CompanyService;
import com.hnkywl.jip.service.SubjectService;
import com.hnkywl.jip.utils.SubjectData;
import net.ruixin.web.struts2.SimpleActionSupport;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
 * 说明：
 * 包:        com.hnkywl.jip.action
 * 类描述:    [一句话描述该类的功能]
 * 创建人:    ZHUCHG
 * 创建时间: 13-3-2 下午4:48
 * 修改人:    ZHUCHG
 * 修改时间: 13-3-2 下午4:48
 * 修改备注: [说明本次修改内容]
 * 版本:        [v1.0]
 */
@Namespace("/cfg")
public class SubjectAction extends SimpleActionSupport {
    private Integer companyId;
    private String data;

    private List<Object[]> gridResult;

    private List<Object[]> companyList;
    private SubjectService subjectService;
    private CompanyService companyService;

    @Action("common_subject_mapping")
    public String commonSubjectMapping() {
        companyList = companyService.searchAllCompany();
        return SUCCESS;
    }

    @Action("common_subject_grid_json")
    public String subjectGridJson() {
        if (companyId != null)
            gridResult = subjectService.findCommonSubject(companyId);
        return SUCCESS;
    }

    @Action("save_subject_grid")
    public void saveSubjectGrid() {
        Gson gson = new Gson();
        java.lang.reflect.Type type = new com.google.gson.reflect.TypeToken<List<SubjectData>>() {
        }.getType();
        List<SubjectData> list = gson.fromJson(this.data, type);
        try {
            subjectService.saveSubjectCode(list);
            renderJson("{\"success\":true,\"message\":\"保存成功!\"}");
        } catch (Exception e) {
            e.printStackTrace();
            renderJson("{\"success\":true,\"message\":\"" + e.getMessage() + "\"}");
        }
    }

    public void setCompanyId(Integer companyId) {
        this.companyId = companyId;
    }

    public List<Object[]> getCompanyList() {
        return companyList;
    }

    public List<Object[]> getGridResult() {
        return gridResult;
    }

    public void setData(String data) {
        this.data = data;
    }

    @Autowired
    public void setSubjectService(SubjectService subjectService) {
        this.subjectService = subjectService;
    }

    @Autowired
    public void setCompanyService(CompanyService companyService) {
        this.companyService = companyService;
    }
}
